using UnityEngine;
using Math;

namespace Render
{
    public static class Colors
    {
        public static int FloatToInt(float f)
        {
            return Maths.Floor(f*255.0f+0.5f);
        }

        public static float IntToFloat(int i)
        {
            return i/255.0f;
        }

        public static float Saturate(float f)
        {
            return (f>1.0f) ? 1.0f:((f<0.0f) ? 0.0f:f);
        }

        public static uint UInt32FromRGBA(byte r, byte g, byte b, byte a)
        {
            return ((uint)r<<24) | ((uint)g<<16) | ((uint)b<<8) | (uint)a;
        }

        public static uint UInt32FromRGBA(int r, int g, int b, int a)
        {
            return ((uint)r<<24) | ((uint)g<<16) | ((uint)b<<8) | (uint)a;
        }

        public static uint UInt32FromRGBA(float r, float g, float b, float a)
        {
            return ((uint)FloatToInt(r)<<24) | ((uint)FloatToInt(g)<<16) | ((uint)FloatToInt(b)<<8) | (uint)FloatToInt(a);
        }

        public static uint UInt32FromColor32(Color32 color32)
        {
            return ((uint)color32.r<<24) | ((uint)color32.g<<16) | ((uint)color32.b<<8) | (uint)color32.a;
        }

        public static void UInt32ToRGBA(uint colorVal, out byte r, out byte g, out byte b, out byte a)
        {
            r = (byte)(colorVal>>24 & 0xff);
            g = (byte)(colorVal>>16 & 0xff);
            b = (byte)(colorVal>> 8 & 0xff);
            a = (byte)(colorVal     & 0xff);
        }

        public static void UInt32ToRGBA(uint colorVal, out int r, out int g, out int b, out int a)
        {
            r = (int)(colorVal>>24 & 0xff);
            g = (int)(colorVal>>16 & 0xff);
            b = (int)(colorVal>> 8 & 0xff);
            a = (int)(colorVal     & 0xff);
        }

        public static void UInt32ToRGBA(uint color, out float r, out float g, out float b, out float a)
        {
            r = IntToFloat((int)(color>>24 & 0xff));
            g = IntToFloat((int)(color>>16 & 0xff));
            b = IntToFloat((int)(color>> 8 & 0xff));
            a = IntToFloat((int)(color     & 0xff));
        }

        public static void UInt32ToColor32(uint color, out Color32 color32)
        {
            color32.r = (byte)(color>>24 & 0xff);
            color32.g = (byte)(color>>16 & 0xff);
            color32.b = (byte)(color>> 8 & 0xff);
            color32.a = (byte)(color     & 0xff);
        }
    }
}